package cn.aitstone.tran;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 创建消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_hello_group");
        // 连接mq
        consumer.setNamesrvAddr("127.0.0.1:9876");
        // 消费者订阅主题
        consumer.subscribe("hello_topic", "hello_tags");
        // 注册监听器
        consumer.registerMessageListener((List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) ->{
                for (MessageExt msg : list){
                    String result = new String(msg.getBody(), StandardCharsets.UTF_8);
                    System.out.println(result);

                }
                // 标记该消息已经成功被消费
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        );
        // 启动
        consumer.start();


    }
}
